/**
 * 
 */
package com.xcn.hadoop.webhdfs;

import java.io.*;
import java.net.*;
import java.security.*;

import javax.swing.text.*;

import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.*;

/**
 * @author changmin
 */
public class Test
{
	
	/**
	 * 
	 */
	public Test( )
	{
	}
	
	/**
	 * 문자열을 SHA-256으로 변환해준다. 16진수는 소문자로 표기
	 * @param buffer
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static String encodeSha256 ( String buffer ) throws NoSuchAlgorithmException
	{
		if ( (buffer != null) && (buffer.length ( ) > 0) )
		{
			MessageDigest md = MessageDigest.getInstance ( "SHA-256" );
			md.reset ( );
			byte byteData[] = md.digest ( buffer.getBytes ( ) );
			
			StringBuffer sb = new StringBuffer ( );
			int cntData = byteData.length;
			for ( int i = 0 ; i < cntData ; i++ )
			{
				sb.append ( Integer.toString ( (byteData[i] & (0xff)) + 0x100, 16 ).substring ( 1 ) );
			}
			return String.valueOf ( sb );
		}
		else
		{
			return "";
		}
	}
	
	/**
	 * @param args
	 * @throws IOException 
	 * @throws IOException 
	 * @throws BadLocationException 
	 * @throws MalformedURLException 
	 * @throws Exception 
	 */
	public static void main ( String[] args )
	{
//		String findSite = "www.nate.com";
//		try
//		{
//			Document doc = Jsoup.connect ( "http://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&ie=utf8&query=" + findSite + "&x=0&y=0" ).get ( );  
//			Elements elements = (( org.jsoup.nodes.Element ) doc).getElementsByAttributeValue ( "class", "cate" );
//			for ( Element e : elements )
//			{
//				System.out.println ( e.text ( ) );
//				//System.out.println ( e.toString ( ) );
//			}
//		}
//		catch ( Exception e )
//		{
//			e.printStackTrace ( );
//		}
		
//		
//		StringUtils.objFixNull ( ax );
//		System.out.println ( StringUtils.encodeSha256 ( "sysadmin" ) );

//		ByteArrayOutputStream os = new ByteArrayOutputStream ( );
//		KerberosWebHDFSConnection _con = new KerberosWebHDFSConnection ( "http://218.234.36.112:8088/edc/index.jsp", "", "" );
//		System.out.println ( _con.open ( "", os ) );
//
//		String message = os.toString ( );
//		System.out.println ( "message" + message );
//		
//		URL url = new URL ( "http://www.naver.com/include/newscast/50.article.json" );
//		HttpURLConnection conn = ( HttpURLConnection ) url.openConnection ( );
//		conn.setRequestMethod ( "GET" );
//		conn.setRequestProperty ( "Content-Type", "application/octet-stream" );
//		conn.connect ( );
//		System.out.println ( conn.getHeaderField ( 0 ) );
//		System.out.println ( conn.getContent ( ) );
//		
//		StringBuffer sb = new StringBuffer ( );
//		if ( conn.getResponseCode ( ) >= 200 && conn.getResponseCode ( ) <= 300 )
//		{
//			InputStream is = conn.getInputStream ( );
//			BufferedReader reader = new BufferedReader ( new InputStreamReader ( is, "utf-8" ) );
//			String line = null;
//			while ( (line = reader.readLine ( )) != null )
//			{
//				sb.append ( line );
//			}
//			reader.close ( );
//			is.close ( );
//		}
//		System.out.println ( sb.toString ( ) );

//		String message = os.toString ( );
//		System.out.println ( message );
//		JSONArray list = ( JSONArray ) JSONSerializer.toJSON ( message );
//		for ( int i = 0 ; i < list.size ( ) ; i++ )
//		{
//			JSONObject item = list.getJSONObject ( i );
//			System.out.println ( item );
//		}
	}
	
	
	
	
	
	
	
	
	
	
}
